﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.Api.Erroc;
using RBAC.Api.Read.Command.RBAC;
using RBAC.Api.Read.Dto;

namespace RBAC.Api.Read.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class UserController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;

        public UserController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public Task<APIResult<LoginDto>> Login(string username, string password)
        {
            LoginCommand command = new LoginCommand();
            command.Username = username;
            command.Password = password;
            return mediator.Send(command);
        }
        
       /// <summary>
       /// 刷新token
       /// </summary>
       /// <param name="command"></param>
       /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<APIResult<TokenResponseDto>> Refresh([FromBody] RefreshTokenQueryCommand command)
        {
            return await mediator.Send(command);
        }
    }
}
